package com.hmy.park.api;

import com.hmy.park.domain.HmyPhouse;
import com.hmy.park.mapper.ApiMapper;
import com.hmy.park.mapper.PhouseMapper;
import com.hmy.park.service.PhouseService;
import com.ruoyi.common.core.web.constant.TableName;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author ljy
 * @Date 2024/2/2 13:17
 * @description: 设备添加更改栏舍信息
 * @Title: AeviceState
 * @Package com.hmy.park.api
 */
@RestController
@RequestMapping("/phouse")
public class AeviceState {
    @Resource
    private ApiMapper apiMapper;
    @Resource
    private PhouseService phouseService;
    @Resource
    private PhouseMapper phouseMapper;

    //@CrossOrigin(origins = "*", allowCredentials = "true", maxAge = 3600)

    /**
     * @param tableName  添加设备的表名
     * @param phouseCoud 绑定的栏舍编码
     * @description 根据绑定栏舍修改栏舍配置信息
     * @author ljy
     * @date 2024/2/2 14:18:23
     */
    @GetMapping("/AeviceState/{tableName}/{phouseCoud}")
    public void getPhouseCoudInTable(@PathVariable("tableName") String tableName,
                                     @PathVariable("phouseCoud") String phouseCoud) {
        Integer phouseCoudInTable = apiMapper.getPhouseCoudInTable(tableName, phouseCoud);
        HmyPhouse phouseByCoud = phouseMapper.findPhouseByCoud(phouseCoud);
        /**
         *  hmy_fans//气体交换机
         * hmy_gas//气体检测仪器
         *  hmy_monitor//监控
         * hmy_wenshi//温度
         */
        HmyPhouse hmyPhouse = new HmyPhouse();

        if (phouseCoudInTable != 0) {
            switch (tableName) {
                case TableName.FANS://气体交换机

                    if (phouseByCoud.getPhouseFans() != 1) {
                        hmyPhouse.setPhouseFans(1);
                        hmyPhouse.setPhouseId(phouseByCoud.getPhouseId());
                        phouseService.updatePhouse(hmyPhouse);
                        System.out.println("-----------------------");
                    }
                    break;
                case TableName.MONITOR://监控
                    if (phouseByCoud.getPhouseMonitor() != 1) {
                        hmyPhouse.setPhouseMonitor(1);
                        hmyPhouse.setPhouseId(phouseByCoud.getPhouseId());
                        phouseService.updatePhouse(hmyPhouse);
                    }
                    break;
                case TableName.WENSHI://温度
                    if (phouseByCoud.getPhouseWenshi() != 1) {
                        hmyPhouse.setPhouseWenshi(1);
                        hmyPhouse.setPhouseId(phouseByCoud.getPhouseId());
                        phouseService.updatePhouse(hmyPhouse);
                    }
                    break;
                case TableName.AUTOFEEDING://自动饲喂
                    if (phouseByCoud.getPhouseAutofeeding() != 1) {
                        hmyPhouse.setPhouseWenshi(1);
                        hmyPhouse.setPhouseId(phouseByCoud.getPhouseId());
                        phouseService.updatePhouse(hmyPhouse);
                    }
                    break;
                case TableName.SCRAPE://刮粪机
                    if (phouseByCoud.getPhouseScrape() != 1) {
                        hmyPhouse.setPhouseScrape(1);
                        hmyPhouse.setPhouseId(phouseByCoud.getPhouseId());
                        phouseService.updatePhouse(hmyPhouse);
                    }
                    break;
                default:
                    new Exception("表名不存在或对应字段不存在");
            }
        } else {
            switch (tableName) {
                case TableName.FANS://气体交换机
                    if (phouseByCoud.getPhouseFans() != 2) {
                        hmyPhouse.setPhouseFans(2);
                        hmyPhouse.setPhouseId(phouseByCoud.getPhouseId());
                        phouseService.updatePhouse(hmyPhouse);
                    }
                    break;
                case TableName.MONITOR://监控
                    if (phouseByCoud.getPhouseMonitor() != 2) {
                        hmyPhouse.setPhouseMonitor(2);
                        hmyPhouse.setPhouseId(phouseByCoud.getPhouseId());
                        phouseService.updatePhouse(hmyPhouse);
                    }
                    break;
                case TableName.WENSHI://温度
                    if (phouseByCoud.getPhouseWenshi() != 2) {
                        hmyPhouse.setPhouseWenshi(2);
                        hmyPhouse.setPhouseId(phouseByCoud.getPhouseId());
                        phouseService.updatePhouse(hmyPhouse);
                    }
                    break;
                case TableName.AUTOFEEDING://自动饲喂
                    if (phouseByCoud.getPhouseAutofeeding() != 2) {
                        hmyPhouse.setPhouseWenshi(2);
                        hmyPhouse.setPhouseId(phouseByCoud.getPhouseId());
                        phouseService.updatePhouse(hmyPhouse);
                    }
                    break;
                case TableName.SCRAPE://刮粪机
                    if (phouseByCoud.getPhouseScrape() != 2) {
                        hmyPhouse.setPhouseScrape(2);
                        hmyPhouse.setPhouseId(phouseByCoud.getPhouseId());
                        phouseService.updatePhouse(hmyPhouse);
                    }
                    break;
                default:
                    new Exception("表名不存在或对应字段不存在");
            }

        }
        hmyPhouse = null;

    }
}
